﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rInvoice.Utils;

namespace rInvoice
{
    public partial class CompanyInputPanel : Form
    {
        private ServerObject mServerObject = null;
        bool mCreatingNew = false;
        bool mIsLoading = false;
        bool mIsActive = true;

        string mCompanyName = string.Empty;
        DateTime mRegistryDate = DateTime.Now;
        string mRegistryNumber = string.Empty;
        string mCodFiscal = string.Empty;
        string mCodTVA = string.Empty;
        string mPhone = string.Empty;
        string mAddress = string.Empty;
        int mCity = 0;
        int mCountryID = 0;
        int mCompanyID = 0;

        public bool CreateNew
        {
            get { return mCreatingNew; }
            set { mCreatingNew = value; }
        }

        public int CompanyID
        {
            get { return mCompanyID; }
            set { mCompanyID = value; }
        }

        public string CompanyNume
        {
            get { return mCompanyName; }
            set { mCompanyName = value; }
        }

        public DateTime RegistryDate
        {
            get { return mRegistryDate; }
            set { mRegistryDate = value; }
        }

        public string RegistryNumber
        {
            get { return mRegistryNumber; }
            set { mRegistryNumber = value; }
        }

        public string CodFiscal
        {
            get { return mCodFiscal; }
            set { mCodFiscal = value; }
        }

        public string CodTVA
        {
            get { return mCodTVA; }
            set { mCodTVA = value; }
        }

        public string Phone
        {
            get { return mPhone; }
            set { mPhone = value; }
        }

        public string Address
        {
            get { return mAddress; }
            set { mAddress = value; }
        }

        public int City
        {
            get { return mCity; }
            set { mCity = value; }
        }

        public int CountryID
        {
            get { return mCountryID; }
            set { mCountryID = value; }
        }

        public bool IsActive
        {
            get { return mIsActive; }
            set { mIsActive = value; }
        }
        
        public CompanyInputPanel()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void FillCityComboBox()
        {
            DataTable cityList = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_CityList);
            cityComboBox.DataSource = cityList;
            cityComboBox.DisplayMember = "Name";
            cityComboBox.ValueMember = "CodeID";
            cityComboBox.SelectedIndex = -1;
        }

        private void FillCountryComboBox()
        {
            DataTable countryList = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_CountryList);
            CountryComboBox.DataSource = countryList;
            CountryComboBox.DisplayMember = "Name";
            CountryComboBox.ValueMember = "CodeID";
            CountryComboBox.SelectedIndex = -1;
        }
        
        private void CompanyInputPanel_Load(object sender, EventArgs e)
        {
            mIsLoading = true;

            FillCityComboBox();
            FillCountryComboBox();

            if (!mCreatingNew)
            {
                companyNameTextBox.Text = mCompanyName;
                registryDateDTPicker.Value = mRegistryDate;
                registryNumberTextBox.Text = mRegistryNumber;
                codIFiscTextBox.Text = mCodFiscal;
                codTVATextBox.Text = mCodTVA;
                phoneTextBox.Text = mPhone;
                addressTextBox.Text = mAddress;
                cityComboBox.SelectedValue = mCity;
                CountryComboBox.SelectedValue = mCountryID;
                activeChekBox.Checked = mIsActive;
            }

            mIsLoading = false;

        }

        private void companyNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mCompanyName = companyNameTextBox.Text;
        }

        private void registryNumberTextBox_TextChanged(object sender, EventArgs e)
        {
            mRegistryNumber = registryNumberTextBox.Text;
        }

        private void registryDateDTPicker_ValueChanged(object sender, EventArgs e)
        {
            if (!mIsLoading)
            {
                mRegistryDate = registryDateDTPicker.Value;
            }
        }

        private void codIFiscTextBox_TextChanged(object sender, EventArgs e)
        {
            mCodFiscal = codIFiscTextBox.Text;
        }

        private void codTVATextBox_TextChanged(object sender, EventArgs e)
        {
            mCodTVA = codTVATextBox.Text;
        }

        private void phoneTextBox_TextChanged(object sender, EventArgs e)
        {
            mPhone = phoneTextBox.Text;
        }

        private void addressTextBox_TextChanged(object sender, EventArgs e)
        {
            mAddress = addressTextBox.Text;
        }

        private void cityComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && cityComboBox.SelectedIndex != -1)
            {
                mCity = (int)cityComboBox.SelectedValue;
            }
        }

        private void CountryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!mIsLoading && CountryComboBox.SelectedIndex != -1)
            {
                mCountryID = (int)CountryComboBox.SelectedValue;
            }
        }

        private void activeChekBox_CheckedChanged(object sender, EventArgs e)
        {
            mIsActive = activeChekBox.Checked;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            bool result = false;

            if (mCreatingNew)
            {
                result = mServerObject.AddCompany(mCompanyName, mRegistryDate, mRegistryNumber, mCodFiscal, mCodTVA, mPhone, mAddress, mCity, mCountryID);
            }
            else
            {
                result = mServerObject.UpdateCompany(mCompanyID,mCompanyName, mRegistryDate, mRegistryNumber, mCodFiscal, mCodTVA, mPhone, mAddress, mCity, mCountryID, !mIsActive);
            }

            if (result)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Abort;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
